package cn.hiapi.publics.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.ActivityCategory;
import cn.hiapi.shopkeeper.service.activity.ActivityCategoryService;
import cn.hiapi.utils.JsonUtils;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/16 22:29
 */
@RestController("publicActivityCategoryController")
@RequestMapping("/public/shop/activity-category")
public class ActivityCategoryController extends BasicController {
    private final DispatchContext dispatchContext;

    public ActivityCategoryController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }


    @GetMapping("")
    public ResponseEntity<List> listCategory() {
        List<ActivityCategory> list = this.dispatchContext.getServiceOne(ActivityCategoryService.class).findList(new QueryWrapper().eq("status", 1), Sort.by(Sort.Direction.DESC, "sort"));
        return toSuccess(
                list.stream().map(e -> JsonUtils.builder().put("id", e.getId()).put("name", e.getName()).build())
                .collect(Collectors.toList())
        );
    }
}
